if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}

auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function 广告() {
  let try_count = 13, close_btn = null;
  while (!close_btn) {
    if (try_count <= 0) {
      break
    }
    // 直到找到关闭按钮
    close_btn = idContains("ad_close").findOne(1000) ||
      className('android.widget.ImageView').clickable().depth(5).drawingOrder(2).findOne(1000)
    if (close_btn) {
      close_btn.click()
      sleep(time)
      var 放弃奖励 = text("放弃奖励").findOne(1000);
      if(放弃奖励) 放弃奖励.click();
      break
    } else {
      try {
        取消 = text("取消").findOne(500)
        if (取消) {
          点击控件(取消)
        }
      } catch (error) { }
      try {
        关闭 = text("关闭").findOne(500)
        if (关闭) {
          点击控件(关闭)
        }
      } catch (error) { }
      try {//支付宝
        if (currentPackage() != 'com.ss.sesame') {
          toastLog('估计是进入支付宝页面了')
          sleep(2000)
          back()
          sleep(2000)
          继续 = textStartsWith("继续").findOne(1000)
          if (继续) {
            点击控件(继续)
          }
          var 每日任务 = text('每日任务').findOne(3000)
          if (每日任务) {
            break
          }
        }
      } catch (error) { }
    }
    try_count--
    sleep(2000)
  }
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function task() {
  var 首页 = text('首页').findOne(30000)
  if (首页) {
    var 倒计时 = id('com.ss.sesame:id/pro_task').findOne(6000)
    if (倒计时) {
      sleep(10000)
      var 领取奖励 = text('领取奖励').findOne(5000)
      if (领取奖励) {
        var finalActivity = currentActivity();
        领取奖励.click()
        var 首页 = text('首页').findOne(1000)
        if (!首页) {
          toastLog('当前是广告界面')
          utils.waitAdvert(finalActivity);
        }
      }
    }
    else {
      toastLog('没有倒计时，等待领取糖果')
    }
  }
  sleep(5000)
  var index = 0
  while (index < 20) {
    var 糖果 = id('com.ss.sesame:id/btn_candy_entrance').findOne(1000)
    if (糖果) {
      糖果.click()
      sleep(time)
      var 领取奖励 = text('领取奖励').findOne(5000)
      if (领取奖励) {
        var finalActivity = currentActivity();
        领取奖励.click()
        sleep(5000)
        var 首页 = text('首页').findOne(1000)
        if (!首页) {
          toastLog('当前是广告界面')
          utils.waitAdvert(finalActivity);
        }
      }
    } else {
      index = 50
    }
    sleep(time)
    index++
    if(index*5 < 100){
      utils.updateAppProgress(index*5);
    }
    
  }
}

function main() {
  var open_app = launchAppEnhance("芝嫲视频")
  if (open_app) {
    var thread_1 = threads.start(function () {
      toastLog("检查广告弹窗")
      while (true) {
        var widget = id("com.ss.sesame:id/check_btn").findOne(2000);
        if (widget) {
          widget.click()
          sleep(time)
          back()
        }
      }
    })
    task()
    thread_1.interrupt()
  }
  else {
    toastLog("芝嫲视频打开失败，请确认是否安装并授权打开!");
  }
}

try {
  utils.updateAppProgress(100);
  main();
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
